{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "removable-australian",
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml\n",
    "\n",
    "import jax\n",
    "from brax.io import html\n",
    "\n",
    "from rl_games.torch_runner import Runner\n",
    "from rl_games.envs.brax import BraxEnv\n",
    "\n",
    "from IPython.display import HTML, IFrame, display, clear_output\n",
    "import os\n",
    "os.environ[\"XLA_PYTHON_CLIENT_PREALLOCATE\"] = \"false\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "realistic-concert",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# Training configs and path to the saved weights of the trained agent\n",
    "\n",
    "# Ant\n",
    "# config_name = 'rl_games/configs/brax/ppo_ant.yaml'\n",
    "# trained_network = 'runs/Ant_brax/nn/Ant_brax.pth'\n",
    "\n",
    "# Humanoid\n",
    "config_name = 'rl_games/configs/brax/ppo_humanoid.yaml'\n",
    "trained_network = 'runs/Humanoid_brax/nn/Humanoid_brax.pth'\n",
    "\n",
    "# config_name = 'rl_games/configs/brax/sac_ant.yaml'\n",
    "# trained_network = 'nn/Ant_brax_sac.pth'\n",
    "\n",
    "# config_name = 'rl_games/configs/brax/sac_humanoid.yaml'\n",
    "# trained_network = 'nn/humanoid_brax_sac.pth'\n",
    "\n",
    "#config_name = 'rl_games/configs/brax/ppo_ur5e.yaml'\n",
    "#trained_network = 'nn/Ur5e_brax.pth'\n",
    "\n",
    "#config_name = 'rl_games/configs/brax/ppo_halfcheetah.yaml'\n",
    "#trained_network = 'nn/Halfcheetah_brax.pth'\n",
    "\n",
    "#config_name = 'rl_games/configs/brax/ppo_grasp.yaml'\n",
    "#trained_network = 'nn/Grasp_brax.pth'\n",
    "\n",
    "#config_name = 'rl_games/configs/brax/ppo_reacher.yaml'\n",
    "#trained_network = 'nn/Reacher_brax.pth'\n",
    "\n",
    "# config_name = 'rl_games/configs/brax/sac_humanoid.yaml'\n",
    "# trained_network = './nn/humanoid_brax_sac.pth'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "russian-howard",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(config_name, 'r') as stream:\n",
    "    config = yaml.safe_load(stream)\n",
    "    runner = Runner()\n",
    "    runner.load(config)\n",
    "    agent = runner.create_player()\n",
    "    agent.restore(trained_network)\n",
    "\n",
    "env_config = runner.params['config']['env_config']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "olive-fitness",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_actors = 1\n",
    "env = BraxEnv('', num_actors, **env_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "imperial-sheet",
   "metadata": {},
   "outputs": [],
   "source": [
    "qps = []\n",
    "obs = env.reset()\n",
    "total_reward = 0\n",
    "num_steps = 0\n",
    "\n",
    "class QP:\n",
    "    def __init__(self, qp):\n",
    "        self.pos = jax.numpy.squeeze(qp.pos, axis=0)\n",
    "        self.rot = jax.numpy.squeeze(qp.rot, axis=0)\n",
    "\n",
    "is_done = False\n",
    "while not is_done:\n",
    "    qps.append(QP(env.env._state.qp))\n",
    "    act = agent.get_action(obs)\n",
    "    obs, reward, is_done, info = env.step(act.unsqueeze(0))\n",
    "    total_reward += reward.item()\n",
    "    num_steps += 1\n",
    "\n",
    "print('Total Reward: ', total_reward)\n",
    "print('Num steps: ', num_steps)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "artistic-prayer",
   "metadata": {},
   "outputs": [],
   "source": [
    "def visualize(sys, qps):\n",
    "    return HTML(html.render(sys, qps))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lovely-alcohol",
   "metadata": {},
   "outputs": [],
   "source": [
    "display(visualize(env.env._env.sys, qps))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "productive-return",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "metadata": {
   "interpreter": {
    "hash": "969772bd26355eec31045f33e535c742a55f971d4e5b984c25fe001e67163e42"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
